﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jayden.Dll.ECMAScript
{
	public interface Expression
	{
		void RenderExpression(StringBuilder builder);
	}

	public interface Operator : Expression
	{
		int Precedence { get; }
	}

	public class ExpressionList : List<Expression>, Expression
	{
		public void RenderExpression(StringBuilder builder)
		{
			for (int c = 0; c < Count; c++)
			{
				if (c > 0)
					builder.Append(", ");
				this[c].RenderExpression(builder);
			}
		}

		public static void RenderOperatorExpression(Operator me, Expression e, StringBuilder builder)
		{
			if (e is Operator)
			{
				Operator op = e as Operator;
				if (op.Precedence > me.Precedence)
				{
					builder.Append("(");
					e.RenderExpression(builder);
					builder.Append(")");
				}
				else
					e.RenderExpression(builder);
			}
			else
				e.RenderExpression(builder);
		}
	}
}
